package com.usian.service.impl;

import com.usian.feign.ApUserFeign;
import com.usian.model.comment.dto.CommentLikeDto;
import com.usian.model.comment.dto.CommentSaveDto;
import com.usian.model.comment.pojo.ApComment;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.service.ApCommentService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import  com.usian.model.comment.pojos.ApCommentLike;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApCommentServiceImpl implements ApCommentService {

    @Autowired
    private ApUserFeign apUserFeign;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public ResponseResult saveComment(CommentSaveDto dto) {

        //1:校验参数  文章id不能空  评论内容长度100
        if(dto==null ||dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getContent().length()>100){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"评论内容长度要小于100");
        }

        //2:获取当前登录用户 本地线程 userid --》 apuser 获取作者对象 头像 昵称
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApUser user = apUserFeign.findById(apUser.getId());
        if(user!=null){
            //3:mongodb 保存评论信息
            ApComment apComment = new ApComment();
            apComment.setAuthorId(user.getId());
            apComment.setAuthorName(user.getName());
            apComment.setContent(dto.getContent());
            apComment.setEntryId(dto.getArticleId());
            apComment.setCreatedTime(new Date());
            apComment.setUpdatedTime(new Date());
            apComment.setImage(apUser.getImage());
            apComment.setLikes(0);
            apComment.setReply(0);
            apComment.setType((short) 0);
            apComment.setFlag((short) 0);
            mongoTemplate.save(apComment);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        return null;
    }

    @Override
    public ResponseResult like(CommentLikeDto dto) {

        //1：校验参数
        if(dto==null ||dto.getCommentId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2: 判断用户是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //3: 点赞 apComment like+1  apcommentLike add     取消点赞  like-1   apcommentLike del
        //Query query =new Query(Criteria.where("id").is(dto.getCommentId()).and("authorId").is(apUser.getId()));
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        if(apComment!=null && dto.getOperation()==1){
            //取消点赞
            if(apComment.getLikes()>0){
                apComment.setLikes(apComment.getLikes()<0 ?  0 : apComment.getLikes()-1);
                mongoTemplate.save(apComment);
            }

            // delete from apCommentLike a where a.commentId=? and a.authorId=?
            Query query =new Query(Criteria.where("commentId").is(dto.getCommentId()).and("authorId").is(apUser.getId()));
            mongoTemplate.remove(query, com.usian.model.comment.pojos.ApCommentLike.class);

        }else{
            //点赞
            apComment.setLikes(apComment.getLikes()+1);
            mongoTemplate.save(apComment);

            Query query=new Query(Criteria.where("authorId").is(apUser.getId()).and("commentId").is(apComment.getId()));
            List<ApCommentLike> commentLikeList = mongoTemplate.find(query, ApCommentLike.class);
            if(commentLikeList.size()==0){
                ApCommentLike apCommentLike =new ApCommentLike();
                apCommentLike.setAuthorId(apUser.getId());
                apCommentLike.setCommentId(apComment.getId());
                apCommentLike.setOperation(dto.getOperation());
                mongoTemplate.save(apCommentLike);
            }
        }
        //4: 返回评论总点赞数量
        Map<String, Object> map =new HashMap<>();
        map.put("likes",apComment.getLikes().toString());
        return ResponseResult.okResult(map);
    }
}
